package com.atguigu.Flink.wordCount;


import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Flink03_StreamBatchWordCount {
    public static void main(String[] args) throws Exception {
        //准备流式环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        System.out.println(env);
        //设置并行度
        env.setParallelism(1);
        //指定运行模式
        /*
        STREAMING: 流处理
        BATCH：     批处理
        AUTOMATIC: 自动选择，根据数据源类型自动确定运行模式，如果是有界流，就用批处理，如果是无界流，就用流处理
         */
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        //从数据源读取数据
        DataStreamSource<String> ds = env.readTextFile("Input/words.txt");
        //转换处理
        //3.1将读取到的一行数据按分隔符进行切分，处理成（word,1）
        SingleOutputStreamOperator<Tuple2<String, Long>> flatMap = ds.flatMap(
                new FlatMapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                        String[] words = value.split(" ");
                        for (String word : words) {
                            out.collect(Tuple2.of(word, 1L));
                        }
                    }
                }
        );
        //3.2根据单词分组
        KeyedStream<Tuple2<String, Long>, String> keyby = flatMap.keyBy(new KeySelector<Tuple2<String, Long>, String>() {
            @Override
            public String getKey(Tuple2<String, Long> value) throws Exception {
                return value.f0;
            }
        });
        //3.3统计每个单词出现的次数
        /*
        sum(int):如果流中数据是tuple，指定tuple的第几个元素用于sum汇总
        sum（strting）：如果流中类型是pojo，指定pojo的哪个属性用于汇总
         */
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = keyby.sum(1);
        //输出结果
        sum.print();
        //启动执行
        env.execute();
    }
}
